// pages/game/game.js
var data = require('../../utils/data.js');
var map = [
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
]
var box = [
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
]
var w = 40;
var row = 0;
var col = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    level:1
  },
  initMap(level){
    let allMap = data.maps[level]
    for(var i = 0;i < 8;i++){
      for(var j = 0;j < 8;j++){
        box[i][j] = 0;
        map[i][j] = allMap[i][j];
        if(allMap[i][j] == 4){
          box[i][j] = 4;
          map[i][j] = 2;
        }else if(allMap[i][j] == 5){
          map[i][j] = 2;
          row = i;
          col = j;
        }
      }
    }
  },
  drawMap(){
    let ctx = this.ctx;
    ctx.clearRect(0, 0, 320, 320);
    for(var i = 0;i < 8;i++){
      for(var j = 0;j < 8;j++){
        let img = 'ice'
        if(map[i][j] == 1){
          img = 'stone'
        }else if(map[i][j] == 3){
          img = 'pig'
        }
        ctx.drawImage('/images/' + img + '.png', j * w, i * w, w, w)
        if(box[i][j] == 4){
          ctx.drawImage('/images/box.png', j * w, i * w, w, w)
        }
      }
    }
    ctx.drawImage('/images/bird.png',col * w, row * w, w, w)

    ctx.draw();

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let level = options.level;
    this.setData({
      level:parseInt(level) + 1
    },()=>{
      this.ctx = wx.createCanvasContext('sokoban')
      this.initMap(level)
      this.drawMap()
    })
    
  },
  up(){
    if(row > 0){
      if(map[row - 1][col] != 1 && box[row - 1][col] != 4){
        row-=1;
      }else if(box[row - 1][col] == 4){
        if(row - 1 > 0){
          if(map[row - 2][col] != 1 && box[row - 2][col] != 4){
            box[row - 2][col] = 4
            box[row - 1][col] = 0
            row -= 1
          }
        }
      }
    }
    this.drawMap();
    this.checkWin();
  },
  down(){
    if(row < 7){
      if(map[row + 1][col] != 1 && box[row + 1][col] != 4){
        row+=1;
      }else if(box[row + 1][col] == 4){
        if(row + 1 < 7){
          if(map[row + 2][col] != 1 && box[row + 2][col] != 4){
            box[row + 2][col] = 4
            box[row + 1][col] = 0
            row += 1
          }
        }
      }
    }
    
    this.drawMap();
    this.checkWin();
  },
  left(){
    if(col > 0){
      if(map[row][col - 1] != 1 && box[row][col - 1] != 4){
        col-=1;
      }else if(box[row][col - 1] == 4){
        if(col - 1 > 0){
          if(map[row][col - 2] != 1 && box[row][col - 2] != 4){
            box[row][col - 2] = 4
            box[row][col - 1] = 0
            col -= 1
          }
        }
      }
    }
    this.drawMap();
    this.checkWin();
  },
  right(){
    if(col < 7){
      if(map[row][col + 1] != 1 && box[row][col + 1] != 4){
        col+=1;
      }else if(box[row][col + 1] == 4){
        if(col + 1 < 7){
          if(map[row][col + 2] != 1 && box[row][col + 2] != 4){
            box[row][col + 2] = 4
            box[row][col + 1] = 0
            col += 1
          }
        }
      }
    }
    this.drawMap();
    this.checkWin();
  },
  isWin(){
    for(var i = 0;i < 8;i++){
      for(var j = 0;j < 8;j++){
        if(box[i][j] == 4 && map[i][j] != 3){
          return false;
        }
      }
    }
    return true;
  },
  checkWin(){
    if(this.isWin()){
      wx.showModal({
        title: '恭喜',
        content: '你获得了胜利',
        showCancel:false
      })
    }
  },
  restart(){
    this.initMap(this.data.level - 1);
    this.drawMap();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})